﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TESTCommons.ConvertTpye;
using TESTCommons.JsonHelper;
using TESTCommons.LinqUtil;
using TestDBData.EFContext;
using TestModel.SysManage;

namespace TestService.SysManage
{
    public partial class Sys_FunctionService : ISys_FunctionService
    {
        #region 参数
        private readonly IRepository<Sys_Function> _functionRepository;//菜单
        #endregion

        #region 构造函数
        public Sys_FunctionService(IRepository<Sys_Function> functionRepository)
        {
            _functionRepository = functionRepository;
        }
        #endregion

        public void AddFunction(Sys_Function model)
        {
            _functionRepository.Insert(model);
        }

        public void DeleteFunction(string ids)
        {
            _functionRepository.Commit(() =>
            {
                ids.DeserializeObject<List<Guid>>().ForEach(it =>
                {
                    var model = _functionRepository.GetById(it.ToGuid());
                    if (model != null)
                    {
                        _functionRepository.Delete(model);
                    }
                });
            });
        }

        public Sys_Function GetModel(Guid gid)
        {
            return _functionRepository.GetById(gid);
        }

        public PagedList<Sys_Function> QueryFunction(Hashtable query, int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var expression = LinqExtensions.True<Sys_Function>();
            if (!string.IsNullOrEmpty(query["function_Name"].ToStr()))
            {
                expression = expression.And(t => t.Function_Name == query["function_Name"].ToStr());
            }
            if (!string.IsNullOrEmpty(query["userName"].ToStr()))
            {
                expression = expression.And(t => t.userName == query["userName"].ToStr());
            }
            var data = _functionRepository.Table.Where(expression.Compile()).ToList();
            return new PagedList<Sys_Function>(data, pageIndex, pageSize);
        }

        public void UpdateFunction(Sys_Function model)
        {
            _functionRepository.Update(model);
        }
    }
}
